import moment from "moment"
// 秒转时分秒
export const getTime = (value, type)=> {
  let secondTime = parseInt(value); // 秒
  let minuteTime = 0; // 分
  let hourTime = 0; // 时
  if (secondTime > 60) {
    //如果秒数大于60，将秒数转换成整数
    //获取分钟，除以60取整，得到整数分钟
    minuteTime = parseInt(secondTime / 60);
    //获取秒数，秒数取余，得到整数秒数
    secondTime = parseInt(secondTime % 60);
    //如果分钟大于60，将分钟转换成小时
    if (type && type === "hour") {
      if (minuteTime > 60) {
        //获取小时，获取分钟除以60，得到整数小时
        hourTime = parseInt(minuteTime / 60);
        //获取小时后取余的分，获取分钟除以60取余的分
        minuteTime = parseInt(minuteTime % 60);
      }
    }
  }
  //若秒数是个位数，前面用0补齐
  secondTime = secondTime < 10 ? "0" + secondTime : secondTime;
  var result = "" + secondTime + "";
  if (minuteTime > 0) {
    //若分钟数是个位数，前面用0补齐
    minuteTime = minuteTime < 10 ? "0" + minuteTime : minuteTime;
    result = "" + minuteTime + ":" + result;
  } else {
    //若分钟数为0，用"00"表示
    result = "" + "00" + ":" + result;
  }
  if (type && type === "hour") {
    if (hourTime > 0) {
      //若小时数是个位数，前面用0补齐
      hourTime = hourTime < 10 ? "0" + hourTime : hourTime;
      result = "" + hourTime + ":" + result;
    } else {
      // 若小时数为0，用"00"表示
      result = "" + "00" + ":" + result;
    }
  }
  // console.log("计时器", result, secondTime)
  return result;
}

// 节流
export const throttle = (func, limit) => {
  let inProgress = false;
  return function() {
    if (!inProgress) {
      inProgress = true;
      setTimeout(() => {
        func.apply(this, arguments);
        inProgress = false;
      }, limit);
    }
  };
}


export const debounce = (fn, wait) => {
  let timeout = null;
  return function() {
    let context = this;
    let args = arguments;
    if (timeout) clearTimeout(timeout);
    let callNow = !timeout;
    timeout = setTimeout(() => {
      timeout = null;
    }, wait);
    if (callNow) fn.apply(context, args);
  };
}

// 时间转换
export const timeConvert = (time) => {
  let min = Math.floor(Number(time)); // 整数部分 分
  let sen = Math.floor((Number(time) - min)*60); // 小数部分 秒
  if (String(sen).length <= 1) {
    sen = "0" + sen
  }
  return {
    min,
    sen
  }
}

// 验证手机号码是否合法
export const checkPhone = (phone) => {
  const regex = /^1[3-9]\d{9}$/;
  return regex.test(phone);
}

// 时间特定格式转换
export const formatTime = (seconds) => {
  // 计算小时数
  const hours = Math.floor(seconds / 3600);
  // 计算分钟数
  const minutes = Math.floor((seconds % 3600) / 60);
  // 计算剩余秒数
  const remainingSeconds = seconds % 60;

  // 小时部分
  const hoursStr = hours > 0 ? `${hours}:` : '';

  // 分钟部分
  const minutesStr = (hours > 0 || minutes > 0) ? `${minutes.toString().padStart(2, '0')}:` : '00:';

  // 秒部分
  const secondsStr = remainingSeconds.toString().padStart(2, '0');

  return `${hoursStr}${minutesStr}${secondsStr}`;
}
// 平均配速格式转换
export const formatSeconds = (seconds) => {
  // 计算分钟数
  const minutes = Math.floor(seconds / 60);
  // 计算剩余秒数
  const remainingSeconds = seconds % 60;

  // 格式化输出
  return `${minutes}'${remainingSeconds}''`;
}
//
export const checkUserInfo = (userInfo) => {
  return userInfo&&userInfo?.headUrl && userInfo?.age && userInfo?.customerName && userInfo?.gender !== null && userInfo?.gender !== '' && userInfo?.height && userInfo?.weight && userInfo?.emergencyContactMoblie
}

// 获取指定年月的所有日期
export const getAllDatesOfMonth = (year, month) => {
  let dates = [];
  const startOfMonth = moment([year, month - 1, 1]);
  const endOfMonth = moment([year, month - 1, startOfMonth.daysInMonth()]);
 
  let currentDate = startOfMonth.clone();
  while (currentDate.isBefore(endOfMonth) || currentDate.isSame(endOfMonth, 'day')) {
    dates.push(currentDate.format('YYYY-MM-DD'));
    currentDate.add(1, 'days');
  }
 
  return dates;
}

export const handleCommonSpeed = (pace) => {
  if (pace > 0) {
    let min = (pace/60).toFixed(2)
    let matchSpeedObj = timeConvert(min)
    return {
      min: matchSpeedObj.min,
      sen: matchSpeedObj.sen,
      pace: `${matchSpeedObj.min}'${matchSpeedObj.sen}"`
    }
  } else {
    return `0'00"`
  }
}
 